﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace VS2010Test
{
    class Dynamic
    {

        #region Constructor
        private static Dynamic _Instance = null;
        private Dynamic()
        {
        }
        static readonly private object padLock = new object();
        public static Dynamic Instance
        {
            get
            {
                if (_Instance == null)
                    lock (padLock)
                    {
                        if (_Instance == null)
                            _Instance = new Dynamic();
                    }
                return _Instance;
            }
        }
        #endregion

        public class Basic
        {

            #region Constructor
            private static Basic _Instance = null;
            private Basic()
            {
            }
            static readonly private object padLock = new object();
            public static Basic Instance
            {
                get
                {
                    if (_Instance == null)
                        lock (padLock)
                        {
                            if (_Instance == null)
                                _Instance = new Basic();
                        }
                    return _Instance;
                }
            }
            #endregion

            public void Example()
            {
                Example1();
                Example2();
                Example3();

                dynamic p;
                p = new Object();
                p = 3;
                p = "";
                p = DateTime.Now;
            
            }

            #region example
            private void Example1()
            {
                dynamic items = new List<string> { "First", "Second", "Third" };
                dynamic valueToAdd = "!";
                foreach (dynamic item in items)
                {
                    string result = item + valueToAdd;
                    Console.WriteLine(result);
                }
            }

            private void Example2()
            {
                dynamic items = new List<string> { "First", "Second", "Third" };
                dynamic valueToAdd = 2;
                foreach (dynamic item in items)
                {
                    string result = item + valueToAdd;
                    Console.WriteLine(result);
                }
            }

            private void Example3()
            {
                dynamic items = new List<int> { 1, 2, 3 };
                dynamic valueToAdd = 2;
                foreach (dynamic item in items)
                {
                    string result = item + valueToAdd;
                    Console.WriteLine(result);
                }
            }
            #endregion

            public void AsReflection()
            {
                object list = new List<string> { "x", "y" };
                object item = "z";
                AddConditionally(list, item);
                AddConditionally((List<string>)list, (string)item);
            }

            private static bool AddConditionallyImpl<T>(IList<T> list, T item)
            {
                if (list.Count < 10)
                {
                    list.Add(item);
                    return true;
                }
                return false;
            }
            private static bool AddConditionally(dynamic list, dynamic item)
            {
                return AddConditionallyImpl(list, item);
            }


        }

    	public class ExpandoObjectsExamples
    	{

    	}		
    }
}
